package com.yc.spring.bank;

import com.yc.cinema.biz.BizException;
import com.yc.spring.bank.bean.Account;
import com.yc.spring.bank.biz.BankBiz;
import com.yc.spring.bank.dao.AccountDao;
import org.apache.ibatis.datasource.DataSourceException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.dao.DataAccessException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = JdbcConfig.class)
public class BankTest {


    @Resource
    AccountDao accountDao;


    @Test
    public void test(){
        try {
            Account account = accountDao.selectById(123456);
            Assert.fail("代码不能执行到这里");
        } catch (DataAccessException e) {
            Assert.assertNotNull(e);
        }
        Account account = new Account();
        account.setMoney(50000d);
        account.setId(123456);
        accountDao.insert(account);

        Assert.assertEquals((Double)50000d,account.getMoney() );


        account.setMoney(60000d);
        accountDao.update(account);

         account = accountDao.selectById(123456);
         Assert.assertEquals((Double)60000d,account.getMoney() );

    }

    @Test
    public void test1(){
        accountDao.deleteById(123456);
    }

    @Resource
    BankBiz bankBiz;
    @Test
    public void test2(){
        Account account = accountDao.selectById(1000000000);
        Double balance = account.getMoney();

        bankBiz.deposit(account.getId(),1000);
        account = accountDao.selectById(1000000000);


        Assert.assertEquals((Double)(balance + 1000),account.getMoney());
    }

    @Test
    public void test3() throws BizException {
        bankBiz.transfer(1000000000,2000000000,1000d);
    }
}
